import * as Cesium from "cesium"
import * as CoordinatesTool from "./CoordinatesTool"

//Cesium多边形转geojson
export function convertPolygonHierarchyToGeoJSON(
  polygonHierarchy: Cesium.PolygonHierarchy,
): GeoJSON.Feature<GeoJSON.Polygon> {
  const outerRing = polygonHierarchy.positions.map((item) =>
    CoordinatesTool.pointToLngLat(item),
  )
  const holes =
    polygonHierarchy.holes?.map((hole) =>
      hole.positions.map((item) => CoordinatesTool.pointToLngLat(item)),
    ) || []
  const coordinates = [outerRing, ...holes]
  return {
    type: "Feature",
    geometry: {
      type: "Polygon",
      coordinates: coordinates,
    },
    properties: {},
  }
}
